{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "gold_data=pd.read_csv('LBMA-GOLD.csv')\n",
    "bit_data=pd.read_csv('BCHAIN-MKPRU.csv')\n",
    "a_g=np.array(np.array(range(1,len(gold_data['Date'])+1)))\n",
    "b_g=np.array(gold_data['Date'])\n",
    "a_b=np.array(np.array(range(1,len(bit_data['Date'])+1)))\n",
    "b_b=np.array(bit_data['Date'])\n",
    "table_g=np.vstack([a_g,b_g]).T\n",
    "table_b=np.vstack([a_b,b_b]).T\n",
    "\n",
    "def dayb2dayg(day_b):\n",
    "    try:\n",
    "        for li in table_b:\n",
    "            if li[0]==day_b:\n",
    "                date=li[1]\n",
    "        for li in table_g:\n",
    "            if li[1]==date:\n",
    "                day_g=li[0]\n",
    "        return day_g,date\n",
    "    except:\n",
    "        return (False,date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "bit_data=np.array(bit_data)\n",
    "gold_data=np.array(gold_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_g=pd.read_csv('prediction_g.csv',header=None)\n",
    "pre_b=pd.read_csv('prediction_b.csv',header=None)\n",
    "pre_g=np.array(pre_g)\n",
    "pre_g[:,0].astype(int)\n",
    "pre_b=np.array(pre_b)\n",
    "pre_b[:,0].astype(int)\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "609.96"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre_b[5-5][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设天数为day_b取该日两产品价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "# today is day_b (day_b>=5)\n",
    "def day_info(day_b):\n",
    "    if day_b<5:\n",
    "        raise ValueError('day_b must bigger than 5')\n",
    "    if dayb2dayg(day_b)[0]!=False:\n",
    "        (day_g,date)=dayb2dayg(day_b)\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        value_g=gold_data[day_g-1][1]\n",
    "        value_g_p=pre_g[day_g-5][1]\n",
    "        value_g_pp=pre_g[day_g-5][2]\n",
    "        info=(True,value_g,value_g_p,value_g_pp,value_b,value_b_p,value_b_pp,date)\n",
    "        return info\n",
    "    else: # gold cannot be treated this day\n",
    "        date=dayb2dayg(day_b)[1]\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        info=(False,value_b,value_b_p,value_b_pp,date)\n",
    "        return info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 1794.6, 1817.6, 1836.1, 46368.69, 47281.0, 48644.0, '9/10/21')"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day_info(1826)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_g=0.01\n",
    "alpha_g=0.02\n",
    "n_c=1000\n",
    "n_g=0\n",
    "n_b=0\n",
    "R=0.05\n",
    "for day_b in range(5,1827):\n",
    "    info=day_info(day_b)\n",
    "    if info[0]==True: # gold can be treated\n",
    "        g_increase1=(info[2]-info[1])/info[1]\n",
    "        g_increase2=(info[3]-info[1])/info[1]\n",
    "        b_increase1=(info[5]-info[4])/info[4]\n",
    "        b_increase2=(info[6]-info[4])/info[4]\n",
    "        if g_increase1<0 and b_increase1<0:\n",
    "            pass\n",
    "        if g_increase1<0 and b_increase1>0:\n",
    "            pass\n",
    "        if g_increase1>0 and b_increase1<0:\n",
    "            pass\n",
    "        if g_increase1>0 and b_increase1>0:\n",
    "            pass                  \n",
    "        # print(g_increase1,b_increase1)\n",
    "    elif info[0]==False:\n",
    "        b_increase1=(info[2]-info[1])/info[1]\n",
    "        b_increase2=(info[3]-info[1])/info[1]\n",
    "        if b_increase1>0:\n",
    "            pass\n",
    "        if b_increase1<0:\n",
    "            pass\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000.0"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info=day_info(1826)\n",
    "sum=n_c+n_g*info[1]+n_b*info[4]\n",
    "sum"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b5fc3e43e814ad01a3162358639356c8d5269fa9e7a0a06af2b9cdf0ba130cad"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
